package com.gitee.candy.string;

import com.gitee.candy.file.FileUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @Author suyanlong
 * @Description TODO
 * @Date 2021/6/15 17:48
 * @Version 1.0
 */
public class StringUtils {

    public static List<Integer> toIntegerList(String str) {
        return toIntegerList(str, ",");
    }

    public static List<Long> toLongList(String str) {
        return toLongList(str, ",");
    }

    public static List<Integer> toIntegerList(String str, String regex) {
        return Arrays.asList(str.split(regex)).stream().map(Integer::valueOf).collect(Collectors.toList());
    }

    public static List<Long> toLongList(String str, String regex) {
        return Arrays.asList(str.split(regex)).stream().map(Long::valueOf).collect(Collectors.toList());
    }

    public static String valueOf(List<Integer> list) {
        return valueOf(list, ",");
    }

    public static String valueOf(List<Integer> list, String delimiter) {
        return list.stream().map(String::valueOf).collect(Collectors.joining(delimiter));
    }

    public static boolean isBlank(String target) {
        return target == null || "".equals(target) || "".equals(target.trim());
    }

    public static boolean isNotBlank(String target) {
        return !isBlank(target);
    }

    public static boolean isEmpty(String target) {
        return target == null || "".equals(target);
    }

    public static boolean isNotEmpty(String target) {
        return !isEmpty(target);
    }

    /**
     * 使用gzip压缩字符串
     */
    public static byte[] compress(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }
        byte[] bytes = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (GZIPOutputStream gzip = new GZIPOutputStream(out)) {
            gzip.write(str.getBytes());
            gzip.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        bytes = out.toByteArray();
        try {
            out.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bytes;
    }

    public static String compressBase64(String str) {
        return compressBase64(compress(str));
    }

    public static String compressBase64(byte[] bytes) {
        return new sun.misc.BASE64Encoder().encode(bytes);
    }

    /**
     * 使用gzip解压缩
     */
    public static String uncompress(byte[] compressed) {
        String decompressed = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(compressed);
            GZIPInputStream ginzip = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return decompressed;
    }

    public static String uncompress(String compressedStr) {
        if (compressedStr == null || compressedStr.length() == 0) {
            return compressedStr;
        }
        String decompressed = null;
        try {
            byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            return uncompress(compressed);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return decompressed;
    }

    public static void main(String[] args) throws Exception {
        String str = "{\"channel\":\"FZLX\",\"data\":{\"applicant\":{\"appliBirthday\":\"1917-04-23\",\"appliEmail\":\"765643146@qq.com\",\"appliIdNo\":\"110101199402012935\",\"appliIdType\":\"01\",\"appliIdentity\":\"1\",\"appliName\":\"霄梵测试巛尹\",\"appliSex\":1,\"id\":60118,\"orderNo\":\"ODCSFZLX20230224144401000005\",\"sendSms\":\"\"},\"insureds\":[{\"delFlag\":1,\"id\":61305,\"insuredBirthday\":\"2001-01-01\",\"insuredIdNo\":\"E66744282\",\"insuredIdType\":\"03\",\"insuredIdentity\":\"9\",\"insuredName\":\"YANG XINYU\",\"insuredOrderNo\":\"255204001\",\"insuredSeqNo\":1,\"myInsuredOrderNo\":\"ODCSFZLX202302241444010000051\",\"orderNo\":\"ODCSFZLX20230224144401000005\",\"sendSms\":\"Y\"}],\"linker\":{\"linkerEmail\":\"765643146@qq.com\",\"linkerName\":\"霄梵测试巛尹\",\"linkerPhone\":\"\"},\"policyInfo\":{\"alipaySerialNo\":\"2023022422001117141441524395\",\"channel\":\"FZLX\",\"channelOrderNo\":\"3220366214034074246\",\"endDate\":1681747200000,\"extendInfo\":{\"arrCity\":\"上海\",\"arrCode\":\"PVG\",\"arrTimePlan\":1681575000000,\"depCity\":\"中国澳门\",\"depCode\":\"MFM\",\"depTimePlan\":1681566300000,\"flightNo\":\"MU9992\"},\"operateTimes\":1677221041000,\"orderNo\":\"ODCSFZLX20230224144401000005\",\"payInfos\":[{\"fee\":50.00,\"payAccountId\":\"2088042116417145\",\"payFlowId\":\"2023022422001117141441524395\",\"payTime\":1677221041000,\"paymentWay\":\"ALIPAY\"}],\"planCode\":\"F00010050\",\"requestSource\":\"\",\"riskCode\":\"008801.accident.abroad.103000\",\"schemePremium\":50.0,\"startDate\":1681488000000}},\"serviceName\":\"tcEffect\",\"sign\":\"t72912t93yn07nr56t475223210688br\",\"timestamp\":1677221041649}";
        String compress = compressBase64(str);
        String uncompress = uncompress(compress);
        FileUtils.saveFile("C:\\Users\\Administrator\\Desktop\\fsdownload\\healthCode-001.log", compress(str));
        byte[] byFile = FileUtils.getGzipBytesByFile("C:\\Users\\Administrator\\Desktop\\fsdownload\\healthCode-001.log");

        String compressBase64 = compressBase64(byFile);
        String uncompress1 = uncompress(compressBase64);
        System.out.println(compress.length());
        System.out.println(uncompress.length());
        System.out.println(compress);
        System.out.println(uncompress);

        System.out.println(isBlank(null));
        System.out.println(isBlank(""));
        System.out.println(isBlank("   "));

        System.out.println(isNotBlank(null));
        System.out.println(isNotBlank(""));
        System.out.println(isNotBlank(" h  "));
    }

}
